package com.ltmonitor.web.controller.video;

import com.ltmonitor.entity.AlarmConfig;
import com.ltmonitor.entity.Department;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.service.IAlarmConfigService;
import com.ltmonitor.service.IOnlineVideoRequestService;
import com.ltmonitor.service.IVehicleDataService;
import com.ltmonitor.util.Constants;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.video.entity.VideoRequest;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.vo.VideoConnection;
import com.ltmonitor.web.vo.VideoServerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 配置报警类型是否启用，报警声音是否启用。
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/videoRequest")
public class VideoRequestAction extends GenericAction {

	@Autowired
	private IOnlineVideoRequestService onlineVideoRequestService;

	@Autowired
	private IVehicleDataService vehicleDataService;

	/**
	 * 获取到当前的视频连接信息和视频服务器内存占用信息
	 * @param m
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model m) {
		List<VideoRequest> res = onlineVideoRequestService.getOnlineVideoRequest();
		m.addAttribute("videoRequestList", res);
		VideoServerInfo vi = onlineVideoRequestService.getVideoServerInfo();
		m.addAttribute("videoServerInfo", vi);
		return "/video/videoConnectionList";

	}

	@ResponseBody
	@RequestMapping("/getMyVideoRequest.action")
	public JsonMessage getMyVideoRequest() {
		try {
			String sessionId = this.getSession().getId();
			List<VideoRequest> ls = onlineVideoRequestService.getMyOwnOnlineVideoRequest(sessionId);
			return json(true, ls);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			return json(false, ex.getMessage());
		}
	}


	/**
	 * 清除用户此前的请求
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/clear.action")
	public JsonMessage clear() {
		try {
			String sessionId = this.getSession().getId();
			onlineVideoRequestService.clearRequest(sessionId);
			return json(true,sessionId);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			return json(false, ex.getMessage());
		}
	}

	/**
	 * 视频监控页面定时发送心跳，证明页面没有被关闭
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/heartBeat.action")
	public JsonMessage heartBeat() {
		try {
			String sessionId = this.getSession().getId();
			onlineVideoRequestService.updateOnlineTime(sessionId);
			return json(true,sessionId);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			return json(false, ex.getMessage());
		}
	}

}
